
package doublelinks;

import java.util.Iterator;

import jss2.DoubleLinkedIterator;
import jss2.DoubleNode;

public class DoubleLinkTester {

	private static <T> DoubleNode <T> getNext (final DoubleNode <T> current) {

		return current.getNext ();
	}
	//testing
	private static <T> boolean hasNext (final DoubleNode <T> current) {

		return current != null;
	}

	private static <T> DoubleNode <T> linkedIterator (
			final DoubleNode <T> firstNode) {

		return firstNode;
	}

	public static void main (final String [] args) {

		System.out.printf ("Lab 4 written by %s%n", SingleLinkTester.YOURNAME);

		final DoubleNode <String> ln1 = new DoubleNode <String> ("apple");
		final DoubleNode <String> ln2 = new DoubleNode <String> ("orange");
		final DoubleNode <String> ln3 = new DoubleNode <String> ("grape");

		System.out.println (ln1);
		System.out.println (ln2);
		System.out.println (ln3);
		System.out.println ();

		ln1.setNext (ln2);
		ln2.setNext (ln3);

		/*
		final Iterator <String> iter = new LinkedIterator <String> (ln1);
		while (iter.hasNext ()) {
			final String temp = iter.next ();
			System.out.println (temp);
		}
		*/

		/*
		{
		DoubleNode<String> current = ln1;
		while (current != null) {
			String temp = current.getElement ();
			current = current.getNext ();
			System.out.println (temp);
		}
		}
		*/

		{
			DoubleNode <String> current = DoubleLinkTester.linkedIterator (ln1);
			while (DoubleLinkTester.hasNext (current)) {
				final String temp = DoubleLinkTester.nextElement (current);
				current = DoubleLinkTester.getNext (current);
				System.out.println (temp);
			}
		}

		System.out.println ();

		ln3.setPrevious (ln2);
		ln2.setPrevious (ln1);
		{
			DoubleNode <String> current = ln3;
			while (current != null) {
				final String temp = current.getElement ();
				current = current.getPrevious ();
				System.out.println (temp);
			}
		}

		System.out.println ();

		{
			final Iterator <String> iter = new DoubleLinkedIterator <String> (
					ln1);
			while (iter.hasNext ()) {
				final String temp = iter.next ();
				System.out.println (temp);
			}
		}

		System.out.println ();

		{
			final DoubleLinkedIterator <String> iter = new DoubleLinkedIterator <String> (
					ln3);
			while (iter.hasPrevious ()) {
				final String temp = iter.previous ();
				System.out.println (temp);

			}
		}
	}

	private static <T> T nextElement (final DoubleNode <T> current) {

		return current.getElement ();
	}

}
